﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.Common;


namespace ESOL.WinUI.Fa
{
    public partial class frmMaintainAsset : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public FaAssetsEntity mSelectedAsset { get; set; }
        private static List<FaAssetsEntity> mtreelist;
        #endregion

        #region construction
        public frmMaintainAsset()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FAS", true);
        }
        private void SetField()
        {
            if (mSelectedAsset == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedAsset.AssetTypeId)==false)
            {
                FaAssetsTypeEntity assetsType = FaAssetsTypeBs.GetFaAssetsTypeByID(mSelectedAsset.AssetTypeId);
                txtAssetType.Text = assetsType.AssetTypeName;
                txtAssetType.Tag = assetsType;
                if (string.IsNullOrEmpty(assetsType.ParentId) == false)
                {
                    FaAssetsTypeEntity assetsType2=FaAssetsTypeBs.GetFaAssetsTypeByID(assetsType.ParentId);
                    txtAssetType02.Text = assetsType2.AssetTypeName;
                    txtAssetType02.Tag = assetsType2;
                }
            }
            if (string.IsNullOrEmpty(mSelectedAsset.ParentAsset) == false)
            {
                FaAssetsEntity asset = FaAssetsBs.GetByID(mSelectedAsset.ParentAsset);
                txtParentAsset.Text = asset.AssetName;
                txtParentAsset.Tag = asset;
            }
            txtAssetCode.Text = mSelectedAsset.AssetCode;
            if (string.IsNullOrEmpty(txtAssetCode.Text))
            {
                txtAssetCode.Text = "自动生成";
            }
            txtAssetSpec.Text = mSelectedAsset.AssetSpec;
            txtAssetName.Text = mSelectedAsset.AssetName;
            txtManufacturer.Text = mSelectedAsset.Manufacturer;
            txtManufacturerCycle.Text = mSelectedAsset.ManufacturerCycle;

            if (mSelectedAsset.PurchaseDate.ToString("yyyy-MM-dd") != "0001-01-01" && mSelectedAsset.PurchaseDate.ToString("yyyy-MM-dd") != "1900-01-01")
            {
                dtpPurchaseDate.EditValue = mSelectedAsset.PurchaseDate;
            }
            else
            {
                dtpPurchaseDate.EditValue = null; 
            }
            cmbStatus.SetSelectedValue(mSelectedAsset.Status);

            if (mSelectedAsset.NeedMaintenance == 1)
            {
                cboxNeedMaintenance.Checked = true;
            }
            else
            {
                cboxNeedMaintenance.Checked = false;
            }
            txtDescription.Text = mSelectedAsset.AssetDescription;
            txtPrice.Text = mSelectedAsset.Price.ToString("#0.00");
            txtProductiongCode.Text = mSelectedAsset.ProductionCode;

            if (mSelectedAsset.ProductionDate.ToString("yyyy-MM-dd") != "0001-01-01" && mSelectedAsset.ProductionDate.ToString("yyyy-MM-dd") != "1900-01-01")
            {
                dtpProductionDate.EditValue = mSelectedAsset.ProductionDate;
            }
            else
            {
                dtpProductionDate.EditValue = null;
            }
            if (mSelectedAsset.NextMaintainDate.ToString("yyyy-MM-dd") != "0001-01-01" && mSelectedAsset.NextMaintainDate.ToString("yyyy-MM-dd") != "1900-01-01")
            {
                dtpNextMaintainDate.EditValue = mSelectedAsset.NextMaintainDate;
            }
            else
            {
                dtpNextMaintainDate.EditValue = null;
            }
            txtMaintainRemark.Text = mSelectedAsset.MaintainRemark;
            txtNextMaintainItem.Text = mSelectedAsset.NextMaintainItem;
        }

        private void SetEntity()
        {
            if (mSelectedAsset == null)
            {
                mSelectedAsset = new FaAssetsEntity();
                mSelectedAsset.AssetId = Guid.NewGuid().ToString();
            }
            mSelectedAsset.AssetTypeId = null;
            if (txtAssetType.Tag != null && txtAssetType.Tag.GetType() == typeof(FaAssetsTypeEntity))
            {
                mSelectedAsset.AssetTypeId = ((FaAssetsTypeEntity)txtAssetType.Tag).AssetTypeId;
            }
            mSelectedAsset.ParentAsset = null;
            if (txtParentAsset.Tag != null && txtParentAsset.Tag.GetType() == typeof(FaAssetsEntity))
            {
                mSelectedAsset.ParentAsset = ((FaAssetsEntity)txtParentAsset.Tag).AssetId;
            }
            mSelectedAsset.AssetCode = txtAssetCode.Text;
            mSelectedAsset.AssetSpec = txtAssetSpec.Text;
            mSelectedAsset.AssetName = txtAssetName.Text;
            mSelectedAsset.Manufacturer = txtManufacturer.Text;
            mSelectedAsset.ManufacturerCycle = txtManufacturerCycle.Text;
            if (dtpPurchaseDate.EditValue!=null)
            {
                mSelectedAsset.PurchaseDate = Convert.ToDateTime(dtpPurchaseDate.EditValue);
            }
            else
            {
                mSelectedAsset.PurchaseDate = Convert.ToDateTime("1900-01-01");
            }
            if (string.IsNullOrEmpty(mSelectedAsset.Status))
            {
                mSelectedAsset.Status = "FAS01";
            }
            if (cboxNeedMaintenance.Checked)
            {
                mSelectedAsset.NeedMaintenance = 1;
            }
            else
            {
                mSelectedAsset.NeedMaintenance = 0;
            }
            mSelectedAsset.AssetDescription = txtDescription.Text;

            mSelectedAsset.Price = CommonFunctions.ToDecimal(txtPrice.Text);
            mSelectedAsset.ProductionCode = txtProductiongCode.Text;
            if (dtpProductionDate.EditValue != null)
            {
                mSelectedAsset.ProductionDate = Convert.ToDateTime(dtpProductionDate.EditValue);
            }
            else
            {
                mSelectedAsset.ProductionDate = Convert.ToDateTime("1900-01-01");
            }
            if (dtpNextMaintainDate.EditValue != null)
            {
                mSelectedAsset.NextMaintainDate = Convert.ToDateTime(dtpNextMaintainDate.EditValue);
            }
            else
            {
                mSelectedAsset.NextMaintainDate = Convert.ToDateTime("1900-01-01");
            }
            mSelectedAsset.MaintainRemark = txtMaintainRemark.Text;
            mSelectedAsset.NextMaintainItem = txtNextMaintainItem.Text;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtAssetType.Text))
            {
                MessageBox.Show("小类不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtAssetCode.Text))
            {
                MessageBox.Show("资产编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtAssetName.Text))
            {
                MessageBox.Show("名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {

            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (this.ThisActionType == ActionType.New)
                {
                    bool successful = FaAssetsBs.Save(true, mSelectedAsset);

                    if (!successful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (this.ThisActionType == ActionType.Modify)
                {
                    bool successful = FaAssetsBs.Save(false, mSelectedAsset);
                    if (!successful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void BindDateGuid()
        {
            if (mSelectedAsset == null||string.IsNullOrEmpty(mSelectedAsset.AssetId))
            {
                return;
            }
            DataSet dsRepairHistory = FaRepairHistoryBs.GetByAsset(mSelectedAsset.AssetId);
            if (dsRepairHistory != null && dsRepairHistory.Tables.Count > 0)
            {
                gvRepairInfo.DataSource = dsRepairHistory.Tables[0];
            }
            DataSet dsMaintainHistory = FaManufacturerHistoryBs.GetByAsset(mSelectedAsset.AssetId);
            if (dsMaintainHistory != null && dsMaintainHistory.Tables.Count > 0)
            {
                gvMaintianInfo.DataSource = dsMaintainHistory.Tables[0];
            }
            DataSet dsUsedHistory = FaUsedHistoryBs.GetByAsset(mSelectedAsset.AssetId);
            if (dsUsedHistory != null && dsUsedHistory.Tables.Count > 0)
            {
                gvUsedInfo.DataSource = dsUsedHistory.Tables[0];
            }
        }

        private void BindTree()
        {
            if (mSelectedAsset == null || string.IsNullOrEmpty(mSelectedAsset.AssetId))
            {
                return;
            }
            mtreelist = new List<FaAssetsEntity>();
            FaAssetsBs.GetAllChildAssets(mSelectedAsset, mtreelist);

            treeAsset.DataSource = mtreelist;
            treeAsset.ExpandAll();


        }
        //private void GetChildAsset(FaAssetsEntity asset, int n)
        //{
        //    if (asset == null || n >= 3)
        //    {
        //        return;
        //    }
        //    n++;
        //    List<FaAssetsEntity> childList = FaAssetsBs.GetChildAsset(asset.AssetId);
        //    if (childList == null || childList.Count == 0)
        //    {
        //        return;
        //    }
        //    foreach (FaAssetsEntity nextChild in childList)
        //    {
        //        GetChildAsset(nextChild, n);
        //    }

        //}
        #endregion

        private void frmMaintainAsset_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetField();
            BindDateGuid();
            BindTree();
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtAssetType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetTypeQuery frm = new frmMaintainAssetTypeQuery();

            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAssetType != null)
            {
                txtAssetType.Text = frm.SelectedAssetType.AssetTypeName;
                txtAssetType.Tag = frm.SelectedAssetType;
                if (string.IsNullOrEmpty(frm.SelectedAssetType.ParentId) == false)
                {
                    FaAssetsTypeEntity pt2 = FaAssetsTypeBs.GetFaAssetsTypeByID(frm.SelectedAssetType.ParentId);
                    txtAssetType02.Text = pt2.AssetTypeName;
                    txtAssetType02.Tag = pt2;
                }
            }
        }

        private void txtParentAsset_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetQuery frm = new frmMaintainAssetQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedAsset != null)
            {
                txtParentAsset.Text = frm.SelectedAsset.AssetName;
                txtParentAsset.Tag = frm.SelectedAsset;
            }
            else 
            {
                txtParentAsset.Text = "";
                txtParentAsset.Tag = null;
            }
        }





    }


}
